package com.crm.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.common.ResVo;
import com.crm.enums.SystemRest;
import com.crm.exception.SystemException;


/**
 * 用于处理所有请求抛出的异常
 * @author dinghao
 *
 */
@ControllerAdvice
public class ExceptionHandle {
	
	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ResVo<?> handle(Exception e) {
		/*
		 * 如果是自定义的异常,返回自定义的异常信息
		 */
		if(e instanceof SystemException) {
			SystemException systemException = (SystemException) e;
			logger.error("系统异常{}",e.toString());
			return ResVo.systemError(systemException.getCode(), systemException.getMessage());
		}
		/*
		 * 未知异常
		 */
		logger.error("未知异常{}",e.toString());
		return ResVo.unKnowError(SystemRest.UNKONW_ERROR);
	}
}
